using UnityEngine;
using System.Collections;
using System.Linq;

public class Aeroplane : MonoBehaviour {

    private float speed = 1.0f;
    private Airport departureAirport;//TODO: Set this when aeroplane departs form airport
    private Airport destinationAirport;
    public Constants.Locations DestinationAirport {
        get {
            return destinationAirport.airportName;
        }set {
            destinationAirport = GetAirportForDestination(value);
        }
    }

    Airport GetAirportForDestination( Constants.Locations destinationName ) {
        Airport[] airports = FindObjectsOfType(typeof(Airport)) as Airport[];
        return airports.First(a => (a as Airport).airportName == destinationName) as Airport;
    }

	// Use this for initialization
	public void TakeOffAeroplane (Airport departureAirport) {
        destinationAirport = GetAirportForDestination(
            (Constants.Locations)Random.Range(0, 3)
        );
        Vector3 departureAirportLocation = departureAirport.transform.position;
        Debug.LogWarning(departureAirportLocation.x + " " + departureAirportLocation.y + " " + departureAirportLocation.z);
        //transform.position = new Vector3(departureAirportLocation.x, Constants.DRAW_ORDER_PLANE, departureAirportLocation.y);
	}

    void Update() {
        if (destinationAirport != null) {
            transform.LookAt(destinationAirport.gameObject.transform.position);
        } else { Debug.LogWarning("Plane without Destination"); }
        //transform.position = Vector3.Lerp(transform, Destination.gameObject.transform, Time.time);
        transform.position = Vector3.MoveTowards(
            transform.position,
            destinationAirport.gameObject.transform.position,
            speed * Time.deltaTime);
    }
	
}
